import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

Vue.use(VueRouter)

const routes = [
  {path: '/', name: 'EntryShow', component: () => import("../views/EntryShow.vue")},
  {path: '/open', name: 'Opening', component: () => import("../views/Opening.vue")},
  {
    path: '/home', name: 'Home', component: () => import("../views/Home.vue"), redirect: '/home/index',
    children: [
      {path: 'index', name: 'Index', component: () => import("../views/Index.vue"), meta: {auth: true}},
      {path: 'discover', name: 'Discover', component: () => import("../views/Discover.vue"), meta: {auth: true}},
      {path: 'skin', name: 'Skin', component: () => import("../views/Skin.vue"), meta: {auth: true}},
      {path: 'mine', name: 'Mine', component: () => import("../views/Mine.vue"), meta: {auth: true}},
    ]
  },
  {path: '/about', name: 'About', component: () => import("../views/About.vue"), meta: {auth: true}},
  {path: '/search', name: 'Search', component: () => import("../views/Search.vue"), meta: {auth: true}},

  {path: '/newArticle', name: 'NewArticle', component: () => import("../views/NewArticle.vue"), meta: {auth: true}},
  {path: '/newReview', name: 'NewReview', component: () => import("../views/NewReview.vue"), meta: {auth: true}},
  {path: '/skinTest', name: 'SkinTest', component: () => import("../views/SkinTest.vue"), meta: {auth: true}},

  {path: '/messages', name: 'Messages', component: () => import("../views/Messages.vue"), meta: {auth: true}},

  {path: '/message/:mid', name: 'Message', component: () => import("../views/Message.vue"), meta: {auth: true}},
  {path: '/production/:pid', name: 'Production', component: () => import("../views/Production.vue"), meta: {auth: true}},
  {path: '/article/:aid', name: 'Article', component: () => import("../views/Article.vue"), meta: {auth: true}},
  {path: '/userInfo/:uid', name: 'UserInfo', component: () => import("../views/UserInfo.vue"), meta: {auth: true}},

  {path: '/register', name: 'Register', component: () => import('../views/Register.vue')},
  {path: '/login', name: 'Login', component: () => import('../views/Login.vue')},
  {path: '/forget', name: 'Forget', component: () => import('../views/Forget.vue')},

  {path: '/util', name: 'Util', component: () => import('../views/Util.vue')},

  {path: '/setting', name: 'Setting', component: () => import('../views/Setting.vue'), meta: {auth: true}},
  {path: '/setting/account', name: 'Account', component: () => import('../views/Account.vue'), meta: {auth: true}},
  {path: '/setting/changePhone', name: 'Phone', component: () => import('../views/ChangePhone.vue'), meta: {auth: true}},
  {path: '/setting/changeEmail', name: 'Email', component: () => import('../views/ChangeEmail.vue'), meta: {auth: true}},
  {path: '/setting/changePassword', name: 'Password', component: () => import('../views/ChangePassword.vue'), meta: {auth: true}},

  {path: '/admin', name: 'Admin', component: () => import("../views/admin/Home.vue"), redirect: '/admin/index',
    children:[
      {path: 'index', component: () => import('../views/admin/Index.vue'),meta: {auth: true}},
      {path: 'menuManage', component: () => import('../views/admin/MenuManage.vue'),meta: {auth: true}},
      {path: 'adminManage', component: () => import('../views/admin/AdminManage.vue'),meta: {auth: true}},
      {path: 'productionManage', component: () => import('../views/admin/ProductionManage.vue'),meta: {auth: true}},
      {path: 'newProduction', component: () => import('../views/admin/NewProduction.vue'),meta: {auth: true}},
      {path: 'editProduction', component: () => import('../views/admin/EditProduction.vue'),meta: {auth: true}},
      {path: 'constantManage', component: () => import('../views/admin/ConstantManage.vue'),meta: {auth: true}},
      {path: 'systemMessageManage', component: () => import('../views/admin/SystemMessageManage.vue'),meta: {auth: true}},
      {path: 'userReportManage', component: () => import('../views/admin/UserReportManage.vue'),meta: {auth: true}},
      {path: 'carouselManage', component: () => import('../views/admin/CarouselManage.vue'),meta: {auth: true}},
      {path: 'newCarousel', component: () => import('../views/admin/NewCarousel.vue'),meta: {auth: true}},
      {path: 'editCarousel', component: () => import('../views/admin/EditCarousel.vue'),meta: {auth: true}},

      {path: '*', name: 'NotFound', component: () => import("../views/NotFound.vue")},
    ]
  },
  {path: '*', name: 'NotFound', component: () => import("../views/NotFound.vue")},
  // {path: '/space/receiving', component: () => import('../views/Receivings.vue'),meta: {auth: true}}, // auth true
  // {path: '/item/:itemid', component: () => import('../views/Item.vue'),meta: {auth: true}}, // auth true
  // {path: '/user/:uuid', component: () => import('../views/User.vue'),meta: {auth: true}}, // auth true
  //
  // {path: '/favourites', component: () => import('../views/Favourite.vue'), meta: {auth: true}},
  // {path: '/chat/:uuid', component: () => import('../views/Chat.vue'), meta: {auth: true}},
  // {path: '/chat', component: () => import('../views/Chat.vue'), meta: {auth: true}},
  // {path: '/orders/:status', component: () => import('../views/Orders.vue'), meta: {auth: true}},
  //
  //
  // {path: '/publish/newpub', component: () => import('../views/PublishNew.vue'), meta: {auth: true}},
  // {path: '/publish', component: () => import('../views/Publish.vue'), meta: {auth: true}},
  // {path: '/publish/:itemid', component: () => import('../views/PublishedItem.vue'),meta: {auth: true}}, // auth true
  // {path: '/publish/modify/:itemid', component: () => import('../views/ItemModify.vue'),meta: {auth: true}}, // auth true
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
})
router.beforeEach((to, from, next) => {
  // 规避循环路由
  if (to.query.redirect===from.path){
    next(to.path)
    return
  }
  // 路由鉴权
  if (to.meta.auth && !store.getters.hasLogin) {
    next({
      path: '/open',
      query: {redirect: to.fullPath}
    })
  } else {
    next()
  }
})

export default router
